<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<title>学生管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.define('webuser', {
        extend: 'Ext.data.Model',
        fields: [
            'id','name','mobile','class_name','openid', 'location_id', 'location_name','class_id'
        ],
        idProperty: 'id'
    });
    
    var store = Ext.create('Ext.data.Store', {
        pageSize: 10,
        model: 'webuser',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/student/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'id'
            }
        ]
    });

    var uclassStore = Ext.create('Ext.data.Store', {
        fields: ['class_id', 'class_name'],  
        remoteSort: true, 
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/class/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        }
    });

    var locationStore = Ext.create('Ext.data.Store', {
        fields: ['location_id', 'location_name'],
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/location/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        }
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "用户ID",
                dataIndex: 'id',
                width: 100
            },{
                text: "姓名",
                dataIndex: 'name',
                width: 150
            },{
                text: '手机号码',
                dataIndex: 'mobile',
                width: 150
            },{
                text: '班级',
                dataIndex: 'class_name',
                width: 150
            },{
                text: '微信名',
                dataIndex: 'openid',
                width: 150
            },{
                text: '地址',
                dataIndex: 'location_name',
                width: 400
            },{
                hidden: true,
                text: '班级id',
                dataIndex: 'class_id'
            },{
                hidden: true,
                text: '地点id',
                dataIndex: 'location_id'
            }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
            afterPageText: "共 {0} 页",
            firstText:"首页",
            lastText:"末页",
            nextText:"下一页",
            prevText:"上一页",
            refreshText:"刷新"
        }),
        tbar: [
            {
                pressed: false,
                icon : '../../img/add.png',
                text: "添  加",
                disable:false,
                handler: addHandler
            },{
                pressed: false,
                icon : '../../img/edit.png',
                text: "修  改",
                disable:false,
                handler: updHandler
            },{
                icon : '../../img/delete.png',
                text : '删  除',
                disable:false,
                handler : delHandler
            },{
                id: 'searchname',
                xtype:'textfield',
                fieldLabel:'姓名',
                //name:'name',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:true,
                tooltip:'按姓名查找',
                //id:'name',
                width: 200
                /*enableKeyEvents:true,
                listeners:{
                   keyup:function(textField, e){
                    if(e.getKey() == 13){   //回车，按姓名查找。

                        var proxyURL = '/student/search_name?name='+Ext.getCmp('searchtxt').getValue();
                        store.getProxy().url = proxyURL;
                        store.loadPage(1);
                    }
                  }
                }*/
            },{
                xtype: "combobox",
                name: "uclass",
                id: 'classcbx',
                fieldLabel: "班级",
                labelAlign: 'right',
                store: uclassStore,
                editable: true,
                displayField: "class_name",
                valueField: "class_id",
                emptyText: "--显示全部--",
                queryMode: "remote",
                triggerAction: "all",
                selectOnFocus : true
                /*forceSelection : true,
                listeners:{
                    'select':function(combo,record,index)
                    {
                        var proxyURL = '/student/filter_classes?uclass='+combo.getRawValue();
                        store.getProxy().url = proxyURL;
                        store.loadPage(1);
                        
                    }
                }*/
            },{
                type: 'button',
                text: '查询',
                handler: function(){
                    var proxyURL = '/student/filter?';
  
                    var selName = Ext.getCmp('searchname').getRawValue();
                    if(selName)
                    {
                         proxyURL = proxyURL + '&user_name=' + selName;
                    }
                    var selclass = Ext.getCmp('classcbx').getValue();
                    if(selclass)
                    {
                         proxyURL = proxyURL + '&user_class=' + selclass;
                    }
                    //alert(proxyURL);
                    store.getProxy().url = proxyURL;
                    store.loadPage(1);
                }              
            }
        ]
    });
    store.loadPage(1);
    uclassStore.load();
    locationStore.load();

    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });

    var addWin;
    var form;
    form = new Ext.FormPanel({
        id: 'myForm',
        url:'/student/add',
        method:'POST',
        frame:true,
        style:'border-width:0 0 0 0;',
        bodyBorder:false,
        bodyPadding: '0 40 0 0',
        layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
        items: [
            {
                xtype:'hidden',
                name:'id',
                id:'id'
            },{
                xtype:'textfield',
                fieldLabel:'学生姓名',
                name:'name',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入姓名',
                id:'name',
                width: 100
            },{
                xtype:'textfield',
                fieldLabel:'手机号码',
                name:'mobile',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入手机号码',
                id:'mobile',
                width: 100
            },{
                xtype: "combobox",
                name: "class",
                id: 'class',
                fieldLabel: "班级",
                labelAlign: 'right',
                store: uclassStore,
                editable: false,
                displayField: "class_name",
                valueField: "class_id",
                emptyText: "--显示全部--",
                queryMode: "remote",
                triggerAction: "all",
                selectOnFocus : true,
                forceSelection : true
            },{
                xtype: 'combobox',
                name: 'location',
                id: 'location',
                fieldLabel: '实习地点',
                labelAlign: 'right',
                store: locationStore,
                editable: false,
                displayField: 'location_name',
                valueField: 'location_id',
                emptyText: '--显示全部--',
                queryMode: 'remote',
                triggerAction: 'all',
                selectOnFocus: true,
                forceSelection: true,
                allowBlank: false
            }
        ],
        buttons:[{ 
            text:'保  存',
            icon:'../../img/accept.png',
            handler:function(){
                if(!form.getForm().isValid()){
                     Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
                     return;
                }
                form.form.submit({
                    waitMsg:'正在保存数据...',
                    success:function(form,action){
                        if(action.result.success){
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg,function(){
                                store.reload();
                                Ext.getCmp('myForm').getForm().reset();
                                addWin.close();
                            });
                        } else {
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg);
                        }
                    },
                    failure:function(form,action){
                        Ext.Msg.alert('信息提示', JSON.parse(action.response.responseText).msg);
                    }
                });
            }
        },{
            text:'关  闭',
            icon:'../../img/close.png',
            handler:function(){
                this.up('form').getForm().reset();
                addWin.close();
            }
        }]
    });
    

    function updHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length > 1){
            Ext.Msg.alert("信息提示","只能选择一条数据进行修改！");
            return;
        } 
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要修改的数据！");
            return;
        }
        form.form.reset();
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '修改用户信息',
                width: 320,
                height: 400,
                constrainHeader: true,
                closeAction:'hide',
                layout: 'fit',
                items: form
            });
        }
        form.form.loadRecord(records[0]);
        form.getForm().findField('class').setValue(records[0].data['class_id']);
        form.getForm().findField('location').setValue(records[0].data['location_id']);
        //Ext.getCmp('user_password').setValue('');
        form.getForm().url='/student/upd';
        addWin.show();
    }
    
    function addHandler(){
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '添加用户信息',
                width: 320,
                height: 400,
                bodyPadding: '0 0 0',
                'z-index': 9999999,
                constrainHeader: true,
                layout: 'fit',
                closeAction:'hide',
                items: form
            });
        }
        form.getForm().url='/student/add';
        addWin.show();
    }
    
    function delHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要删除的数据！");
            return;
        }
        Ext.MessageBox.confirm('信息提示','确认删除吗？',function(val){
            if(val == 'yes'){
                Ext.Ajax.request({
                    url: '/student/del?id='+records[0].get("id"),
                    method : 'POST',
                    success: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        Ext.Msg.alert('信息提示',data.msg);
                        store.reload();
                    },
                    failure: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        if(!data.success){
                            Ext.Msg.alert('信息提示',data.msg);
                        } else {
                            Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                        }
                    }
                });
            }
        });
    }

});
</script>
<body>
</body>
</html>
